package com.heima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.heima.dao.OrderSettingDao;
import com.heima.pojo.OrderSetting;
import com.heima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> list) {

        if (list!=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                long count= orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count>0){
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }

    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date+"-1";
        String end = date+"-31";
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();
        if (list!=null && list.size()>0){
            for (OrderSetting orderSetting : list) {
                HashMap<String,Object> hashMap = new HashMap<>();
                hashMap.put("date",orderSetting.getOrderDate().getDate());
                hashMap.put("number",orderSetting.getNumber());
                hashMap.put("reservations",orderSetting.getReservations());
                result.add(hashMap);
            }
        }
        return result;

    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if (count>0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
